//
//  AboutUsViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/12/11.
//  Copyright © 2016年 com.xtion.crm. All rights reserved.
//

#import "AboutUsViewController.h"
#import "CollectionViewCell.h"
#import "AboutUsDetailViewController.h"
#import "FeedBackViewController.h"

NSString const *AboutUsViewControllerTitleParamKey = @"AboutUsViewControllerTitleParamKey";

@interface AboutUsViewController ()
{
    NSMutableDictionary *_MenuIndex;
    UIFont *_titleFont;
    UILabel *_nameLabel;
    NSString *_feedBackStr;
    NSString *_aboutUsStr;
    NSString *_aboutUsStrTitle;
}
@end

@implementation AboutUsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkSession];
    
    self.view.backgroundColor = [UIColor colorWithHex:mcrmSAASUIShinColorTextStyle2196F3Color];
    
    [self initVariable];
    [self initUI];
    [self initMenu];
    
    [self.view addSubview:self.collectionView];

}

- (void)initVariable
{
    _aboutUsStrTitle = self.param[AboutUsViewControllerTitleParamKey];
    _feedBackStr = @"意见反馈";
    _aboutUsStr = @"关于我们";
    _MenuIndex = [NSMutableDictionary new];
}

- (void)initUI
{
    if(_nameLabel == nil)
    {
        CGFloat height = self.view.height/3 + 50;
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, height, self.view.width - 60, 50)];
        _nameLabel.text = _aboutUsStr;
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.font = [UIFont systemFontOfSize:35];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        
        [self.view addSubview:_nameLabel];
    }
}

- (void)initMenu
{
    NSInteger index = 0;
    
    MenuInfo *feedBackInfo = [MenuInfo new];
    feedBackInfo.pId = @"1";
    feedBackInfo.code = _feedBackStr;
    feedBackInfo.name = _feedBackStr;
    
    [_MenuIndex setObject:feedBackInfo forKey:[NSString stringWithFormat:@"%@", @(index++)]];
    
    MenuInfo *aboutUsInfo = [MenuInfo new];
    aboutUsInfo.pId = @"1";
    aboutUsInfo.code = _aboutUsStr;
    aboutUsInfo.name = _aboutUsStr;
    
    [_MenuIndex setObject:aboutUsInfo forKey:[NSString stringWithFormat:@"%@", @(index++)]];
}
#pragma mark - 创建collectionView并设置代理
- (UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, _nameLabel.bottom + 10, self.view.width, 0) collectionViewLayout:flowLayout];
        _collectionView.height = self.view.height  -  (_nameLabel.bottom + 10);
        
        //定义每个UICollectionView 的大小
        if([UIDevice is40InchScreen] == YES)
        {
            flowLayout.itemSize = CGSizeMake(80,80);
            _titleFont = [UIFont systemFontOfSize:10];
        }
        else
        {
            flowLayout.itemSize = CGSizeMake(100,100);
            _titleFont = [UIFont systemFontOfSize:15];
        }
        //定义每个UICollectionView 横向的间距
        flowLayout.minimumLineSpacing = 5;
        //定义每个UICollectionView 纵向的间距
        flowLayout.minimumInteritemSpacing = 0;
        //定义每个UICollectionView 的边距距
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 5, 5);//上左下右
        
        //注册cell
        [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        
        //设置代理
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        //背景颜色
        _collectionView.backgroundColor = [UIColor whiteColor];
        //自适应大小
        _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        _collectionView.alwaysBounceVertical = YES;
        
    }
    return _collectionView;
}

#pragma mark - UICollectionView delegate dataSource
#pragma mark 定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _MenuIndex.count;
}

#pragma mark 定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark 每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    [cell sizeToFit];
    
    MenuInfo *info = [_MenuIndex valueForKey:[NSString stringWithFormat:@"%@",@(indexPath.item)]];
    
    if(info != nil)
    {
        cell.szTag = info.menuId;
        cell.title = info.name;
        
        UIImage *headImage = nil;
        headImage = [UIImage imageNamed:@"mine_setting.png"];
        
        headImage = [UIImage resizeImage:headImage withNewSize:CGSizeMake(100, 100)];
        
        cell.bgImg = headImage;
        
        cell.titleFont = _titleFont;
    }
    return cell;
}

#pragma mark UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MenuInfo *info = [_MenuIndex valueForKey:[NSString stringWithFormat:@"%@", @(indexPath.item)]];
    
    if([info.code isEqualToString:_feedBackStr])
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[FeedBackViewControllerTitleParamKey] = _feedBackStr;

        [XUINavigationController pushViewControllerClass:[FeedBackViewController class]];
    }
    else if([info.code isEqualToString:_aboutUsStr])
    {
        [XUINavigationController pushViewControllerClass:[AboutUsDetailViewController class]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
